package br.unifor.lec.aco;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import br.unifor.lec.aco.graph.Vertice;
import br.unifor.lec.aco.util.CalculaR2;

public class Statistic {

	public Map<Integer, Integer> hash = new HashMap<Integer, Integer>();

	public void register(int vertice) {
		Integer frequence = hash.remove(vertice);
		if (frequence == null) {
			frequence = 0;
		}

		frequence++;
		hash.put(vertice, frequence);
	}

	@SuppressWarnings("unchecked")
	public String print() {
		Set<Integer> keys = hash.keySet();
		Iterator<Integer> i = keys.iterator();
		Vector y = new Vector(keys.size());
		double x[] = new double[keys.size()];
		int j=0;
		String out = "";
		Integer key;

		while (i.hasNext()) {
			key = i.next();
			y.add(Math.log10(hash.get(key)));
			x[j]=Math.log10(j+1);
			j++;
			out += "V" + key + "  " + hash.get(key)
					+ "\n";
		//	out+=hash.get(key)	+ "\n";
		}
		
		Comparator comparator = Collections.reverseOrder();
		Collections.sort(y, comparator);
		Iterator<Double> iterator= y.iterator();
		double vetorY[]= new double[keys.size()];
		int k=0;
		while (iterator.hasNext()) {
			vetorY[k]=iterator.next();
			k++;
		}
		
		double alpha = CalculaR2.calcAlpha(x, vetorY);
		double R2 = CalculaR2.calcR2(alpha, x, vetorY);
		String printR2 = "R2= "+R2;
		printR2=printR2.replace(".", ",");
		System.out.println(printR2);
		String printAlfa = "Alfa="+alpha;
		printAlfa=printAlfa .replace(".", ",");
		System.out.println(printAlfa );
		
		return out;
	}

	public Map<Integer, Integer> getHash() {
		return hash;
	}

	public void setHash(Map<Integer, Integer> hash) {
		this.hash = hash;
	}
}
